#pragma once
#include <iostream>
#include <fstream>
#include <ctime>
#include "boss.h"
#include "Manager.h"
#include "General_staff.h"
#include "worker.h"
using namespace std;
class Administrator
{
public:
    string using_name = "zhou";  // 管理员账号
    string pass_word = "123456"; // 管理员账号密码
    int worker_num=0;//职工人数
    worker **worke;//职工数组指针
    Administrator(){
        this->worker_num=0;//初始化职工人数为0
        this->worke=NULL;//初始化职工数组
    }
    // 菜单
    void menu()
    {
        cout << "*********************************************" << endl;
        cout << "******** 欢迎使用xx公司职工管理系统 *********" << endl;
        cout << "************* 1,增加职工信息 ****************" << endl;
        cout << "************* 2,显示职工信息 ****************" << endl;
        cout << "************* 3,删除离职职工 ****************" << endl;
        cout << "************* 4,修改职工信息 ****************" << endl;
        cout << "************* 5,查找职工信息(按照姓名) ******" << endl;
        cout << "************* 6,清空管理系统 ****************" << endl;
        cout << "*************** 7,保存数据 ******************" << endl;        
        cout << "************* 0,退出管理程序 ****************" << endl;
        cout << "*********************************************" << endl;
    }
    // 登录验证函数
    bool Login_verification()
    {
        string using_name;
        string pass_word;
        cout << "请输入账号>>";
        cin >> using_name;
        cout << "请输入密码>>";
        cin >> pass_word;
        if (this->pass_word == pass_word && this->using_name == using_name)
        {
            cout << "登录成功" << endl;
            system("pause");
            system("cls");
            return true;
        }
        else
        {
            cout << "账号或密码不对" << endl;
            return false;
        }
    }
    // 职工增加函数
    void Increase_staff(){
        int new_worker_num,add_worker_num;
        cout<<"请输入增加的职工数量>>";cin>>add_worker_num;
        if(add_worker_num>0){
            //重新计算职工指针的数组大小
            new_worker_num=this->worker_num+add_worker_num;
            worker **new_worke=new worker*[new_worker_num];
            //原有的数据放在新建的数组中
            if(!(new_worke!=NULL)){
                for(int i=0;i<this->worker_num;i++){
                    new_worke[i]=this->worke[i];
                }
            }
            //创建新的职工对象
            for(int i=0;i<add_worker_num;i++){
                int type;
                string add_name,add_deptip;
                int add_id;
                cout<<"请输入职工类型(1,普通员工;2,经理;3,CEO)>>";cin>>type;
                cout<<"请输入职工姓名>>";cin>>add_name;
                cout<<"请输入职工id>>";cin>>add_id;
                cout<<"请输入职工部门>>";cin>>add_deptip;
                worker* wo=NULL;
                switch (type){
                case 1:
                    wo=new General_staff(add_name,add_id,add_deptip);
                    break;
                case 2:
                    wo=new Manager(add_name,add_id,add_deptip);
                    break;
                case 3:
                    wo=new boss(add_name,add_id,add_deptip);
                    break;
                default:
                    break;
                }
                new_worke[this->worker_num+i]=wo;
            }
            delete[] this->worke;
            this->worke=new_worke;
            this->worker_num=new_worker_num;
            cout<<"职工添加完成"<<endl;
        }
        else{
            cout<<"输入的数字不合法"<<endl;
        }
    }
    // 职工信息输出函数
    void Output_staff(){
        if(this->worker_num==0){
            cout<<"管理系统中没有职工信息"<<endl;
        }
        else{
            for(int i=0;i<this->worker_num;i++){
                worke[i]->showinfo();
            }
            cout<<"职工信息展示完成"<<endl;
        }
    }
    // 职工信息修改函数
    void Modify_staff(){
        string find_name;
        cout<<"请输入想要修改的职工名字>>"<<endl;cin>>find_name;
        int num=Search_staff(find_name);
        if(num!=-1){
            int type;
            cout<<"请输入想要修改的信息(1,姓名;2,id;3,部门)>>";cin>>type;
            switch (type)
            {
            case 1:
                cout<<"请输入修改之后的姓名>>";cin>>this->worke[num]->name;
                break;
            case 2:
                cout<<"请输入修改之后的id>>";cin>>this->worke[num]->id;
                break;
            case 3:
                cout<<"请输入修改之后的部门>>";cin>>this->worke[num]->deptip;
                break;
            default:
                break;
            }
            cout<<"修改完成"<<endl;
        }
        else{
            cout<<"职工不在管理系统中，修改失败"<<endl;
        }
    }
    // 职工信息删除函数
    void Delete_staff(){
        string find_name;
        cout<<"请输入你想要删除的职工姓名>>";cin>>find_name;
        int num=Search_staff(find_name);
        if(num!=-1){
            for(int i=num;i<this->worker_num;i++){
                this->worke[i]=this->worke[i+1];
            }
            this->worker_num--;
        }
        else{
            cout<<"这个职工不在管理系统中，无法删除"<<endl;
        }
    }
    // 职工信息查询函数
    int Search_staff(string name){
        for(int i=0;i<this->worker_num;i++){
            if(this->worke[i]->name==name){
                return i;
            }
        }
        return -1;
    }
    // 管理系统清空函数
    void clear() {
        // 清空职工数组
        for (int i = 0; i < worker_num; i++) {
            delete worke[i];
        }   
        delete[] worke;
        // 重置职工人数和数组指针
        worker_num = 0;
        worke = NULL;
        cout << "管理系统已清空" << endl;
    }
    //数据写入文件
    void save(){
        time_t time_now=time(0);
        tm *tml=localtime(&time_now);
        fstream fs("worker.txt",ios::app);
        if(fs.is_open()==false){
            cout<<"文件打开失败"<<endl;
            return;
        }
        else{
            for(int i=0;i<this->worker_num;i++){
                fs<<"名字："<<this->worke[i]->name
                <<"\tid："<<this->worke[i]->id
                <<"\t部门："<<this->worke[i]->deptip
                <<"\t职工类型："<<this->worke[i]->worker_type
                <<endl;
            }
            fs<<"职工人数:"<<this->worker_num<<endl;
            fs<<"修改时间："<<1900+tml->tm_year<<"/"
            <<1+tml->tm_mon<<"/"
            <<tml->tm_mday<<"/"
            <<tml->tm_hour<<":"
            <<tml->tm_min<<":"
            <<tml->tm_sec<<endl;
            cout<<"数据保存完成"<<endl;
        }
    }
};